<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/hexo/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/hexo/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/hexo/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/hexo/images/logo.svg" color="#222">

<link rel="stylesheet" href="/hexo/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/blue/pace-theme-bounce.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"codeleader.gitee.io","root":"/hexo/","images":"/hexo/images","scheme":"Gemini","darkmode":true,"version":"8.14.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/hexo/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/hexo/js/config.js"></script>

    <meta name="description" content="Java集合 常用的集合类 集合类存放于java.util包中，主要有3种：List、Set(包含Queue)、Map  image-20230708115413707   image-20230708115434822   注意：Collection接口不是Map的父接口">
<meta property="og:type" content="article">
<meta property="og:title" content="Java集合复习">
<meta property="og:url" content="https://codeleader.gitee.io/hexo/2023/07/08/Java%E9%9B%86%E5%90%88%E5%A4%8D%E4%B9%A0/index.html">
<meta property="og:site_name" content="CodeLeader">
<meta property="og:description" content="Java集合 常用的集合类 集合类存放于java.util包中，主要有3种：List、Set(包含Queue)、Map  image-20230708115413707   image-20230708115434822   注意：Collection接口不是Map的父接口">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708115413707.png">
<meta property="og:image" content="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708115434822.png">
<meta property="og:image" content="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708164303190.png">
<meta property="og:image" content="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708164340649.png">
<meta property="og:image" content="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708174345789.png">
<meta property="og:image" content="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708174317589.png">
<meta property="article:published_time" content="2023-07-08T09:54:05.000Z">
<meta property="article:modified_time" content="2023-07-09T01:18:53.958Z">
<meta property="article:author" content="别团等shy哥发育">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Java集合">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708115413707.png">


<link rel="canonical" href="https://codeleader.gitee.io/hexo/2023/07/08/Java%E9%9B%86%E5%90%88%E5%A4%8D%E4%B9%A0/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://codeleader.gitee.io/hexo/2023/07/08/Java%E9%9B%86%E5%90%88%E5%A4%8D%E4%B9%A0/","path":"2023/07/08/Java集合复习/","title":"Java集合复习"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Java集合复习 | CodeLeader</title>
  








  <noscript>
    <link rel="stylesheet" href="/hexo/css/noscript.css">
  </noscript>
<style>mjx-container[jax="SVG"] {
  direction: ltr;
}

mjx-container[jax="SVG"] > svg {
  overflow: visible;
}

mjx-container[jax="SVG"][display="true"] {
  display: block;
  text-align: center;
  margin: 1em 0;
}

mjx-container[jax="SVG"][justify="left"] {
  text-align: left;
}

mjx-container[jax="SVG"][justify="right"] {
  text-align: right;
}

g[data-mml-node="merror"] > g {
  fill: red;
  stroke: red;
}

g[data-mml-node="merror"] > rect[data-background] {
  fill: yellow;
  stroke: none;
}

g[data-mml-node="mtable"] > line[data-line] {
  stroke-width: 70px;
  fill: none;
}

g[data-mml-node="mtable"] > rect[data-frame] {
  stroke-width: 70px;
  fill: none;
}

g[data-mml-node="mtable"] > .mjx-dashed {
  stroke-dasharray: 140;
}

g[data-mml-node="mtable"] > .mjx-dotted {
  stroke-linecap: round;
  stroke-dasharray: 0,140;
}

g[data-mml-node="mtable"] > svg {
  overflow: visible;
}

[jax="SVG"] mjx-tool {
  display: inline-block;
  position: relative;
  width: 0;
  height: 0;
}

[jax="SVG"] mjx-tool > mjx-tip {
  position: absolute;
  top: 0;
  left: 0;
}

mjx-tool > mjx-tip {
  display: inline-block;
  padding: .2em;
  border: 1px solid #888;
  font-size: 70%;
  background-color: #F8F8F8;
  color: black;
  box-shadow: 2px 2px 5px #AAAAAA;
}

g[data-mml-node="maction"][data-toggle] {
  cursor: pointer;
}

mjx-status {
  display: block;
  position: fixed;
  left: 1em;
  bottom: 1em;
  min-width: 25%;
  padding: .2em .4em;
  border: 1px solid #888;
  font-size: 90%;
  background-color: #F8F8F8;
  color: black;
}

foreignObject[data-mjx-xml] {
  font-family: initial;
  line-height: normal;
  overflow: visible;
}

.MathJax path {
  stroke-width: 3;
}

mjx-container[display="true"] {
  overflow: auto hidden;
}

mjx-container[display="true"] + br {
  display: none;
}
</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/hexo/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">CodeLeader</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">欲买桂花同载酒，终不似少年游</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/hexo/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/hexo/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/hexo/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">19</span></a></li><li class="menu-item menu-item-categories"><a href="/hexo/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">4</span></a></li><li class="menu-item menu-item-archives"><a href="/hexo/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">17</span></a></li><li class="menu-item menu-item-schedule"><a href="/hexo/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a></li><li class="menu-item menu-item-sitemap"><a href="/hexo/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/hexo/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#java%E9%9B%86%E5%90%88"><span class="nav-number">1.</span> <span class="nav-text">Java集合</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E7%9A%84%E9%9B%86%E5%90%88%E7%B1%BB"><span class="nav-number">1.1.</span> <span class="nav-text">常用的集合类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#list"><span class="nav-number">1.2.</span> <span class="nav-text">List</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#arraylist%E5%8A%A8%E6%80%81%E6%95%B0%E7%BB%84"><span class="nav-number">1.2.1.</span> <span class="nav-text">ArrayList(动态数组)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#linkedlist%E5%8F%8C%E5%90%91%E9%93%BE%E8%A1%A8"><span class="nav-number">1.2.2.</span> <span class="nav-text">LinkedList(双向链表)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vector%E5%8A%A8%E6%80%81%E6%95%B0%E7%BB%84%E5%AE%9E%E7%8E%B0%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8"><span class="nav-number">1.2.3.</span> <span class="nav-text">Vector(动态数组实现、线程安全)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#set"><span class="nav-number">1.3.</span> <span class="nav-text">Set</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#hashset%E5%93%88%E5%B8%8C%E8%A1%A8"><span class="nav-number">1.3.1.</span> <span class="nav-text">HashSet(哈希表)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#linkedhashsethashsetlinkedhashmap"><span class="nav-number">1.3.2.</span> <span class="nav-text">LinkedHashSet(HashSet+LinkedHashMap)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#treeset%E4%BA%8C%E5%8F%89%E6%A0%91"><span class="nav-number">1.3.3.</span> <span class="nav-text">TreeSet(二叉树)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#map"><span class="nav-number">1.4.</span> <span class="nav-text">Map</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#hashmap%E6%95%B0%E7%BB%84%E9%93%BE%E8%A1%A8%E7%BA%A2%E9%BB%91%E6%A0%91"><span class="nav-number">1.4.1.</span> <span class="nav-text">HashMap(数组+链表+红黑树)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#linkedhashmap%E8%AE%B0%E5%BD%95%E6%8F%92%E5%85%A5%E9%A1%BA%E5%BA%8F"><span class="nav-number">1.4.2.</span> <span class="nav-text">LinkedHashMap(记录插入顺序)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#hashtable%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8"><span class="nav-number">1.4.3.</span> <span class="nav-text">HashTable(线程安全)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#treemap%E5%8F%AF%E6%8E%92%E5%BA%8F"><span class="nav-number">1.4.4.</span> <span class="nav-text">TreeMap(可排序)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#concurrenthashmap%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8"><span class="nav-number">1.4.5.</span> <span class="nav-text">ConcurrentHashMap(线程安全)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#listsetmap%E4%B8%89%E8%80%85%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-number">1.5.</span> <span class="nav-text">List、Set、Map三者的区别？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E9%9B%86%E5%90%88%E6%A1%86%E6%9E%B6%E7%9A%84%E5%BA%95%E5%B1%82%E5%AE%9E%E7%8E%B0"><span class="nav-number">1.6.</span> <span class="nav-text">常用集合框架的底层实现</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="别团等shy哥发育"
      src="https://codeleader.oss-cn-beijing.aliyuncs.com/site/avatar.jpg">
  <p class="site-author-name" itemprop="name">别团等shy哥发育</p>
  <div class="site-description" itemprop="description">别团等shy哥发育的博客</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/hexo/archives/">
          <span class="site-state-item-count">17</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/hexo/categories/">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/hexo/tags/">
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/xiongtete0519" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;xiongtete0519" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/hexo/xiongtete0519@gmail.com" title="E-Mail → xiongtete0519@gmail.com" rel="noopener me"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://codeleader.blog.csdn.net/" title="CSDN → https:&#x2F;&#x2F;codeleader.blog.csdn.net&#x2F;" rel="noopener me" target="_blank"><i class="fa CSDN fa-fw"></i>CSDN</a>
      </span>
  </div>

        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="返回顶部">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://codeleader.gitee.io/hexo/2023/07/08/Java%E9%9B%86%E5%90%88%E5%A4%8D%E4%B9%A0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://codeleader.oss-cn-beijing.aliyuncs.com/site/avatar.jpg">
      <meta itemprop="name" content="别团等shy哥发育">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="CodeLeader">
      <meta itemprop="description" content="别团等shy哥发育的博客">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Java集合复习 | CodeLeader">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java集合复习
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-07-08 17:54:05" itemprop="dateCreated datePublished" datetime="2023-07-08T17:54:05+08:00">2023-07-08</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-07-09 09:18:53" itemprop="dateModified" datetime="2023-07-09T09:18:53+08:00">2023-07-09</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/hexo/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>1.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>6 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="java集合">Java集合</h1>
<h2 id="常用的集合类">常用的集合类</h2>
<p>集合类存放于<code>java.util</code>包中，主要有3种：List、Set(包含Queue)、Map</p>
<figure>
<img data-src="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708115413707.png" alt=""><figcaption>image-20230708115413707</figcaption>
</figure>
<figure>
<img data-src="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708115434822.png" alt=""><figcaption>image-20230708115434822</figcaption>
</figure>
<blockquote>
<p>注意：Collection接口不是Map的父接口</p>
</blockquote>
<span id="more"></span>
<h2 id="list">List</h2>
<p>List是有序的Collection,Java中List一共三个实现类：<code>ArrayList</code>、<code>Vector</code>和<code>LinkedList</code>。</p>
<h3 id="arraylist动态数组">ArrayList(动态数组)</h3>
<p><code>ArrayList</code><strong>是用数组实现的，允许对元素进行快速随机访问</strong>。ArrayList类在添加一个新元素时，如果现有数组的容量不够，则会将数组长度扩容为原来的1.5倍之后再添加。</p>
<blockquote>
<p>动态数组扩容并不是在原有连续的内存空间后进行简单的叠加，而是重新申请一块更大的新内存，并把现有容器中的元素逐个复制过去，然后销毁就的内存。</p>
</blockquote>
<h3 id="linkedlist双向链表">LinkedList(双向链表)</h3>
<p><strong>LinkedList是顺序访问结构，内部使用双向链表实现</strong>。查询数据会消耗一定时间(需要遍历链表进行查询)。<strong>适合数据的动态插入和删除</strong>，随机访问和遍历速度较慢。可以当作堆、栈、队列和双向队列使用。</p>
<h3 id="vector动态数组实现线程安全">Vector(动态数组实现、线程安全)</h3>
<p><strong>Vector和<code>ArrayList</code>一样，也是通过动态数组实现，不同的是它是线程安全的，即某一时刻只有一个线程能够读写Vector，访问速度比<code>ArrayList</code>慢</strong>。</p>
<h2 id="set">Set</h2>
<p>Set最主要的特点是集合中的元素不能重复，存入Set的每个元素都必须定义<code>equals()</code>方法来确保对象的唯一性。</p>
<blockquote>
<p>对象的相等性本质是通过<code>hashCode</code>值(Java是根据对象的内存地址计算处次序号)判断的，如果想要让两个不同对象视为相等的，就必须覆盖Object类的<code>hashCode</code>和equals方法。</p>
</blockquote>
<h3 id="hashset哈希表">HashSet(哈希表)</h3>
<p>基于哈希表实现，哈希表中存访的是哈希值。<code>HashSet</code>存储元素的顺序是按照哈希值来存的所以取数据也是按照哈希值取的，元素的哈希值是通过元素的<code>hashCode</code>方法来实现的。</p>
<blockquote>
<p><code>HashSet</code>和LinkedHashSet集合判断两个元素相等的标准是两个对象通过<code>hashCode</code>方法比较，并且两个对象的equals方法返回值也相等。</p>
<ul>
<li>HashSet不会有重复的元素</li>
<li>HashSet中最多只允许有一个<code>null</code></li>
<li>HashSet不是线程安全</li>
<li>HashSet不会维护数据插入的顺序，也不会对数据进行排序。</li>
</ul>
</blockquote>
<h3 id="linkedhashsethashsetlinkedhashmap">LinkedHashSet(HashSet+LinkedHashMap)</h3>
<p><code>LinkedHashSet</code>是<code>HashSet</code>的扩展，<code>HashSet</code>并不维护数据的顺序，而<code>LinkedHashSet</code>维护了数据插入的顺序。</p>
<h3 id="treeset二叉树">TreeSet(二叉树)</h3>
<p>TreeSet基于二叉树对新添加的对象按照指定的顺序(升序、降序)，没添加一个对象都会进行排序，并将对象插入二叉树指定的位置。</p>
<blockquote>
<p>Integer和String等基础对象类型可以直接根据TreeSet的默认排序进行存储。</p>
<p>定制排序：如果自然排序不符合当前业务的需求，或者元素的类型没有实现<code>Comparable</code>接口，那么在创建TreeSet时，可以单独指定一个定制比较器<code>Comparator</code>的实现类对象，使用定制比较器的TreeSet判断两个元素相等的标准是通过<code>compare(Object o1,Object o2)</code>方法比较两个元素返回了<code>0</code>。</p>
</blockquote>
<h2 id="map">Map</h2>
<p>Map是一种由多组key-value(键值对)集合在一起的结构，其中key是不能重复的。</p>
<blockquote>
<p>在Map中存储数据，实际上是将<span class="math inline"><mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.464ex;" xmlns="http://www.w3.org/2000/svg" width="12.178ex" height="2.034ex" role="img" focusable="false" viewBox="0 -694 5382.6 899"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D458" d="M121 647Q121 657 125 670T137 683Q138 683 209 688T282 694Q294 694 294 686Q294 679 244 477Q194 279 194 272Q213 282 223 291Q247 309 292 354T362 415Q402 442 438 442Q468 442 485 423T503 369Q503 344 496 327T477 302T456 291T438 288Q418 288 406 299T394 328Q394 353 410 369T442 390L458 393Q446 405 434 405H430Q398 402 367 380T294 316T228 255Q230 254 243 252T267 246T293 238T320 224T342 206T359 180T365 147Q365 130 360 106T354 66Q354 26 381 26Q429 26 459 145Q461 153 479 153H483Q499 153 499 144Q499 139 496 130Q455 -11 378 -11Q333 -11 305 15T277 90Q277 108 280 121T283 145Q283 167 269 183T234 206T200 217T182 220H180Q168 178 159 139T145 81T136 44T129 20T122 7T111 -2Q98 -11 83 -11Q66 -11 57 -1T48 16Q48 26 85 176T158 471L195 616Q196 629 188 632T149 637H144Q134 637 131 637T124 640T121 647Z"></path></g><g data-mml-node="mi" transform="translate(521,0)"><path data-c="1D452" d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z"></path></g><g data-mml-node="mi" transform="translate(987,0)"><path data-c="1D466" d="M21 287Q21 301 36 335T84 406T158 442Q199 442 224 419T250 355Q248 336 247 334Q247 331 231 288T198 191T182 105Q182 62 196 45T238 27Q261 27 281 38T312 61T339 94Q339 95 344 114T358 173T377 247Q415 397 419 404Q432 431 462 431Q475 431 483 424T494 412T496 403Q496 390 447 193T391 -23Q363 -106 294 -155T156 -205Q111 -205 77 -183T43 -117Q43 -95 50 -80T69 -58T89 -48T106 -45Q150 -45 150 -87Q150 -107 138 -122T115 -142T102 -147L99 -148Q101 -153 118 -160T152 -167H160Q177 -167 186 -165Q219 -156 247 -127T290 -65T313 -9T321 21L315 17Q309 13 296 6T270 -6Q250 -11 231 -11Q185 -11 150 11T104 82Q103 89 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mo" transform="translate(1754.8,0)"><path data-c="2192" d="M56 237T56 250T70 270H835Q719 357 692 493Q692 494 692 496T691 499Q691 511 708 511H711Q720 511 723 510T729 506T732 497T735 481T743 456Q765 389 816 336T935 261Q944 258 944 250Q944 244 939 241T915 231T877 212Q836 186 806 152T761 85T740 35T732 4Q730 -6 727 -8T711 -11Q691 -11 691 0Q691 7 696 25Q728 151 835 230H70Q56 237 56 250Z"></path></g><g data-mml-node="mi" transform="translate(3032.6,0)"><path data-c="1D463" d="M173 380Q173 405 154 405Q130 405 104 376T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Q21 294 29 316T53 368T97 419T160 441Q202 441 225 417T249 361Q249 344 246 335Q246 329 231 291T200 202T182 113Q182 86 187 69Q200 26 250 26Q287 26 319 60T369 139T398 222T409 277Q409 300 401 317T383 343T365 361T357 383Q357 405 376 424T417 443Q436 443 451 425T467 367Q467 340 455 284T418 159T347 40T241 -11Q177 -11 139 22Q102 54 102 117Q102 148 110 181T151 298Q173 362 173 380Z"></path></g><g data-mml-node="mi" transform="translate(3517.6,0)"><path data-c="1D44E" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path></g><g data-mml-node="mi" transform="translate(4046.6,0)"><path data-c="1D459" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path></g><g data-mml-node="mi" transform="translate(4344.6,0)"><path data-c="1D462" d="M21 287Q21 295 30 318T55 370T99 420T158 442Q204 442 227 417T250 358Q250 340 216 246T182 105Q182 62 196 45T238 27T291 44T328 78L339 95Q341 99 377 247Q407 367 413 387T427 416Q444 431 463 431Q480 431 488 421T496 402L420 84Q419 79 419 68Q419 43 426 35T447 26Q469 29 482 57T512 145Q514 153 532 153Q551 153 551 144Q550 139 549 130T540 98T523 55T498 17T462 -8Q454 -10 438 -10Q372 -10 347 46Q345 45 336 36T318 21T296 6T267 -6T233 -11Q189 -11 155 7Q103 38 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mi" transform="translate(4916.6,0)"><path data-c="1D452" d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z"></path></g></g></g></svg></mjx-container></span>的数据存储在<code>Map.Entry</code>接口的实例中，再在Map集合中插入<code>Map.Entry</code>的实例化对象。</p>
</blockquote>
<h3 id="hashmap数组链表红黑树">HashMap(数组+链表+红黑树)</h3>
<p>HashMap根据键的hashCode值存访数据，大多数情况下可以直接定位到它的值，因而具有很快的访问速度，但遍历顺序确实不确定的。</p>
<blockquote>
<p>HashMap最多只允许一条记录的键为null，允许多条记录的值为null</p>
<p>HashMap非线程安全，可以使用Collections的synchronizedMap方法使HashMap具有线程安全的能力</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">HashMap&lt;Object, Object&gt; map = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;&gt;();</span><br><span class="line">Map&lt;Object, Object&gt; synchronizedMap = Collections.synchronizedMap(map);</span><br></pre></td></tr></table></figure>
<p>或者使用ConcurrentHashMap。</p>
</blockquote>
<p>Jdk7及之前的HashMap结构：<strong>数组+链表</strong></p>
<figure>
<img data-src="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708164303190.png" alt=""><figcaption>image-20230708164303190</figcaption>
</figure>
<p>JDK1.8的HashMap结构：<strong>数据+链表+红黑树</strong></p>
<figure>
<img data-src="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708164340649.png" alt=""><figcaption>image-20230708164340649</figcaption>
</figure>
<h3 id="linkedhashmap记录插入顺序">LinkedHashMap(记录插入顺序)</h3>
<p>LinkedHashMap是HashMap的子类，它维护了一个双向链表，此链表定义了迭代顺序，此迭代顺序通常就是将键插入映射中的顺序。</p>
<h3 id="hashtable线程安全">HashTable(线程安全)</h3>
<p>HashMap和HashTable都是哈希表，二者区别如下：</p>
<table>
<thead>
<tr class="header">
<th style="text-align: center;">表</th>
<th style="text-align: center;">底层结构</th>
<th style="text-align: center;">线程安全(同步)</th>
<th style="text-align: center;">版本</th>
<th style="text-align: center;">效率</th>
<th style="text-align: center;">key,value是否允许为null</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">HashMap</td>
<td style="text-align: center;">哈希表</td>
<td style="text-align: center;">不安全(不同步)</td>
<td style="text-align: center;">较新</td>
<td style="text-align: center;">较高</td>
<td style="text-align: center;">允许</td>
</tr>
<tr class="even">
<td style="text-align: center;">HashTable</td>
<td style="text-align: center;">哈希表</td>
<td style="text-align: center;">安全(同步)</td>
<td style="text-align: center;">较老</td>
<td style="text-align: center;">较低下</td>
<td style="text-align: center;">不允许</td>
</tr>
</tbody>
</table>
<h3 id="treemap可排序">TreeMap(可排序)</h3>
<p>TreeMap的集合是基于红黑树(Red-Black Tree)的可导航<code>NavigableMap</code>实现的。TreeMap中的映射关系要么根据其key键的自然顺序进行排序，要么根据创建TreeMap对象时提供给key键的定制排序<code>Comparator</code>接口实现类进行排序，具体取决于使用的构造方法。</p>
<h3 id="concurrenthashmap线程安全">ConcurrentHashMap(线程安全)</h3>
<p>ConcurrentHashMap是HashMap中支持高并发、高吞吐量的线程安全的版本。它由<code>Segment</code>数组结构和<code>HashEntry</code>数组结构组成。Segment在ConcurrentHashMap中扮演锁的角色，HashEntry则用于存储键值对数据。</p>
<p><strong>JDK1.7中ConcurrentHashMap实现</strong>：JDK1.7中ConcurrentHashMap采用分段锁的思想实现并发操作，因此是线程安全的。ConcurrentHashMap有多个Segment组成(Segment的数量也是锁的并发度)，每个Segment均继承于ReentrantLock并单独枷锁，所以每次进行加锁操作时锁住的都是一个Segment，这样只要保证每个Segment都是线程安全的，也就实现了全局的线程安全。</p>
<figure>
<img data-src="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708174345789.png" alt=""><figcaption>image-20230708174345789</figcaption>
</figure>
<p><strong>JDK1.8中ConcurrentHashMap的实现</strong>：JDK1.8中ConcurrentHashMap启用了Segment分段锁，改用<code>Synchronized+CAS</code>实现对多线程的安全操作。同时，JDK1.8在ConcurrentHashMap中引入了红黑树。</p>
<figure>
<img data-src="https://codeleader.oss-cn-beijing.aliyuncs.com/site/image-20230708174317589.png" alt=""><figcaption>image-20230708174317589</figcaption>
</figure>
<h2 id="listsetmap三者的区别">List、Set、Map三者的区别？</h2>
<ul>
<li><code>List</code>:有序集合(有序指存入的顺序和取出的顺序相同，不是按照元素的某些特性排序)，可存储重复元素，可存储多个null。(<code>LinkedList、ArrayList、Vector</code>)</li>
<li><code>Set</code>:无序集合(元素存入和取出顺序不一定相同)，集合中的元素不能重复，只能存储一个null。(<code>HashSet、TreeSet</code>)</li>
<li><code>Map</code>:使用键值对的方式对元素进行存储，<code>key</code>是唯一的，不能重复，value可以重复。(<code>HashMap、TreeMap、LinkedHashMap、WeakHashMap、IdentityHashMap</code>)</li>
</ul>
<h2 id="常用集合框架的底层实现">常用集合框架的底层实现</h2>
<ul>
<li><p>List:</p>
<p><code>ArrayList</code>:数组</p>
<p><code>LinkedList</code>:双向链表</p></li>
<li><p>Set:</p>
<p><code>HashSet</code>:底层基于<code>HashMap</code>实现，<code>HashSet</code>存入读取元素的方式和<code>HashMap</code>中的key是一致的。</p>
<p><code>TreeSet</code>:红黑树</p></li>
<li><p>Map:</p>
<p><code>HashMap</code>:JDK1.8之前<code>HashMap</code>由<strong>数组+链表</strong>组成，<code>JDK1.8</code>之后由<strong>数组+链表/红黑树</strong>组成，当链表长度大于8时，链表转化为红黑树。这样做的目的是能提高<code>HashMap</code>的性能，因为红黑树的查找元素的时间复杂度远小于链表。</p>
<p><code>HashTable</code>:数组+链表</p>
<p><code>TreeMap</code>:红黑树</p></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>别团等shy哥发育
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://codeleader.gitee.io/hexo/2023/07/08/Java%E9%9B%86%E5%90%88%E5%A4%8D%E4%B9%A0/" title="Java集合复习">https://codeleader.gitee.io/hexo/2023/07/08/Java集合复习/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/hexo/tags/Java/" rel="tag"># Java</a>
              <a href="/hexo/tags/Java%E9%9B%86%E5%90%88/" rel="tag"># Java集合</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/hexo/2023/07/08/Java%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E5%A4%8D%E4%B9%A0/" rel="prev" title="Java基础知识复习">
                  <i class="fa fa-chevron-left"></i> Java基础知识复习
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/hexo/2023/08/11/%E5%BF%AB%E6%89%8B%E4%B8%80%E9%9D%A2/" rel="next" title="快手一面">
                  快手一面 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2022 – 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">别团等shy哥发育</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">81k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">4:56</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

<!-- <br /> -->
<!-- 网站运行时间的设置 -->
<span id="timeDate">载入天数...</span>
<!-- <span id="times">载入时分秒...</span> -->
<script>
    var now = new Date();
    function createtime() {
        var grt= new Date("11/17/2022 8:00:00");//此处修改你的建站时间或者网站上线时间
        now.setTime(now.getTime()+250);
        days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
        hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
        if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
        mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
        seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
        snum = Math.round(seconds); 
        if(String(snum).length ==1 ){snum = "0" + snum;}
        // var times = document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
        document.getElementById("timeDate").innerHTML = "本站已安全运行 "+dnum+" 天 "+hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
    }
setInterval("createtime()",250);
</script>

    </div>
  </footer>

  

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/hexo/js/comments.js"></script><script src="/hexo/js/utils.js"></script><script src="/hexo/js/motion.js"></script><script src="/hexo/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/hexo/js/third-party/search/local-search.js"></script>




  <script src="/hexo/js/third-party/pace.js"></script>

  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/hexo/js/third-party/math/mathjax.js"></script>



</body>
</html>
